<?php
abstract class Dealscombined_SiteProcessor_ProcessorAbstract implements Dealscombined_SiteProcessor_ProcessorInterface{
	
	protected $_database;	
	protected $_url;	
	protected $_feedUrl;
	protected $_feedPath;
	
	public function __construct(Zend_Db_Adapter_Abstract $database, $url = null, $feedUrl = null, $feedPath = null)
	{
		$this->_database = $database;		
		$this->_url = $url;
		$this->_feedUrl = $feedUrl;
		$this->_feedPath = $feedPath;
	}			
		
	protected function normaliseUrl($url, $baseUrl) 
	{
		return $baseUrl.$url;
	}
	
	public function process($userAgent = 'Googlebot/2.1 (http://www.googlebot.com/bot.html)'){}
	
	public function getXpath($rawData){
		$dom = new DOMDocument();
		@$dom -> loadHTML($rawData);
			
		// grab all the on the page
		return  new DOMXPath($dom);
	}
	
	public function getRawData($targetUrl, $userAgent = 'Googlebot/2.1 (http://www.googlebot.com/bot.html)')
	{
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
		curl_setopt($ch, CURLOPT_URL, $targetUrl);
		curl_setopt($ch, CURLOPT_FAILONERROR, true);
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
		curl_setopt($ch, CURLOPT_AUTOREFERER, true);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($ch, CURLOPT_TIMEOUT, 10);
		$html = curl_exec($ch);
		
		if ($html) {
			$result = $html;				
		} else{													
			echo "<br />cURL error number:" . curl_errno($ch);
			echo "<br />cURL error:" . curl_error($html);
			$result = false;
		}	
		
		return $result;
	}
	
	public function getXML($filePath = null){
		if (file_exists($filePath)) {
    		$xml = simplexml_load_file($filePath);
			return $xml;
		}	
		else {
 		   throw new Exception("'Failed to open $filePath", 1);
		}
	}
	
}